package com.cy.train.business.service.imp;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.cy.train.business.domain.TrainCarriage;
import com.cy.train.business.domain.TrainSeat;
import com.cy.train.business.domain.TrainSeatExample;
import com.cy.train.business.enums.SeatColEnum;
import com.cy.train.business.mapper.TrainSeatMapper;
import com.cy.train.business.req.TrainSeatQueryReq;
import com.cy.train.business.req.TrainSeatSaveReq;
import com.cy.train.business.resp.TrainSeatQueryResp;
import com.cy.train.business.service.TrainCarriageService;
import com.cy.train.business.service.TrainSeatService;
import com.cy.train.common.resp.PageResp;
import com.cy.train.common.util.SnowUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Date: 2024/2/27 14:48
 * @Author: cyzhang
 */
@Service
public class TrainSeatServiceImp implements TrainSeatService {
    private static final Logger LOG = LoggerFactory.getLogger(TrainSeatServiceImp.class);

    @Autowired
    private TrainSeatMapper trainSeatMapper;

    @Autowired
    private TrainCarriageService trainCarriageService;

    @Override
    public void save(TrainSeatSaveReq req) {
        DateTime now = DateTime.now();
        TrainSeat trainSeat = BeanUtil.copyProperties(req, TrainSeat.class);
        if(ObjectUtil.isNull(trainSeat.getId())) {
            trainSeat.setId(SnowUtil.getSnowflakeNextId());
            trainSeat.setCreateTime(now);
            trainSeat.setUpdateTime(now);
            trainSeatMapper.insert(trainSeat);
        } else {
            trainSeat.setUpdateTime(now);
            trainSeatMapper.updateByPrimaryKeySelective(trainSeat);
        }

    }

    @Override
    public PageResp<TrainSeatQueryResp> queryList(TrainSeatQueryReq req) {
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.setOrderByClause("id desc");
        TrainSeatExample.Criteria criteria = trainSeatExample.createCriteria();
        if (ObjectUtil.isNotEmpty(req.getTrainCode())) {
            criteria.andTrainCodeEqualTo(req.getTrainCode());
        }

        LOG.info("当前页：{}", req.getPage());
        LOG.info("每页数量：{}", req.getSize());

        PageHelper.startPage(req.getPage(), req.getSize());
        List<TrainSeat> trainSeatList = trainSeatMapper.selectByExample(trainSeatExample);


        PageInfo<TrainSeat> pageInfo = new PageInfo<>(trainSeatList);
        LOG.info("总条数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<TrainSeatQueryResp> trainSeatQueryRespList = BeanUtil.copyToList(trainSeatList, TrainSeatQueryResp.class);
        PageResp<TrainSeatQueryResp> pageResp = new PageResp<>();
        pageResp.setTotal(pageInfo.getTotal());
        pageResp.setList(trainSeatQueryRespList);

        return pageResp;
    }

    @Override
    public void delete(Long id) {
        trainSeatMapper.deleteByPrimaryKey(id);
    }

    @Transactional
    @Override
    public void generateTrainSeat(String trainCode) {
        //判断当前车次是否有座位信息，若有则清空所有座位信息
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        trainSeatMapper.deleteByExample(trainSeatExample);

        // 查找当前车次下的所有车厢
        List<TrainCarriage> list = trainCarriageService.selectByTrainCode(trainCode);

        // 循环生成每个车厢的座位
        for(TrainCarriage trainCarriage : list) {
            // 拿到车厢数据：行数、列数、座位类型
            // 根据车厢座位类型，筛选出所有的列
            Integer rowCount = trainCarriage.getRowCount();
            String seatType = trainCarriage.getSeatType();
            List<SeatColEnum> seatColList = SeatColEnum.getColsByType(seatType);
            Integer colCount = seatColList.size();
            // 循环行数
            for(int i = 0; i < rowCount; i++) {
                // 循环列数
                for(int j = 0; j <colCount; j++) {
                    // 构造座位数据并保存数据库
                    DateTime now = new DateTime();
                    TrainSeat trainSeat = new TrainSeat();
                    trainSeat.setId(SnowUtil.getSnowflakeNextId());
                    trainSeat.setTrainCode(trainCode);
                    trainSeat.setSeatType(seatType);
                    trainSeat.setRow(StrUtil.fillBefore(String.valueOf(i), '0', 2));
                    trainSeat.setCol(seatColList.get(j).getCode());
                    trainSeat.setCarriageIndex(trainCarriage.getIndex());
                    trainSeat.setCarriageSeatIndex(i * colCount + j + 1);
                    trainSeat.setCreateTime(now);
                    trainSeat.setUpdateTime(now);
                    trainSeatMapper.insert(trainSeat);
                }
            }
        }
    }

    @Override
    public List<TrainSeat> queryTrainSeatByCode(String trainCode) {
        TrainSeatExample trainSeatExample = new TrainSeatExample();
        trainSeatExample.setOrderByClause("`id` desc");
        trainSeatExample.createCriteria().andTrainCodeEqualTo(trainCode);
        return trainSeatMapper.selectByExample(trainSeatExample);
    }
}
